package com.camel.base.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.camel.base.model.ZsWorkRecord;
import com.camel.base.model.ZsWorkShift;
import com.camel.base.service.ZsWorkRecordService;
import com.camel.base.service.ZsWorkShiftService;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ResultUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author camel
 */
@RestController
@RequestMapping("/zsWorkShift")
public class ZsWorkShiftController extends BaseCommonController<ZsWorkShift> {

    @Autowired
    private ZsWorkShiftService<ZsWorkShift> service;

    @Autowired
    private ZsWorkRecordService workRecordService;

    @Override
    public boolean deleteAble(Serializable id) {
        ZsWorkShift byId = service.getById(id);
        if (ObjectUtils.isEmpty(byId)) {
            throw new CustomException("删除失败: " + getMouduleName() + "不存在");
        }
        return true;
    }

    /**
     * 可用调查
     * @return
     * @throws ParseException
     */
    @GetMapping("/active")
    public Result active() throws ParseException {
        // 今日日期
        String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        Date startDate = DateUtils.parseDate(format + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtils.parseDate(format + " 23:59:59", "yyyy-MM-dd HH:mm:ss");

        // 今天的班次
        List<ZsWorkShift> workShifts = service.list(new QueryWrapper<ZsWorkShift>()
                .ge("shift_date", startDate)
                .lt("shift_date", endDate));
        if (CollectionUtils.isEmpty(workShifts)) {
            return ResultUtil.success(new ArrayList<>());
        }
        List<Integer> collect = workShifts.stream().map(ZsWorkShift::getId).collect(Collectors.toList());
        List<ZsWorkRecord> records = workRecordService.list(new QueryWrapper<ZsWorkRecord>().in("shift_id", collect)
                .eq("creator", StpUtil.getLoginIdAsInt()).eq("result", 1));

        if (CollectionUtils.isEmpty(records)) {
            return ResultUtil.success(new ArrayList<>());
        }
        ArrayList<ZsWorkShift> zsWorkShifts = new ArrayList<>();
        for (ZsWorkRecord record : records) {
            for (ZsWorkShift workShift : workShifts) {
                if (record.getShiftId().equals(workShift.getId())) {
                    zsWorkShifts.add(workShift);
                    break;
                }
            }
        }
        return ResultUtil.success(zsWorkShifts);
    }

    @Override
    public BaseCommonService<ZsWorkShift> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "班次";
    }
}
